<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <DeptTdqqTree @onDeptIdChanged="onDeptIdChanged">
        </DeptTdqqTree>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="农户代码" prop="nhdm">
            <el-input
              v-model="queryParams.nhdm"
              placeholder="请输入农户代码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="户主姓名" prop="hzxm">
            <el-input
              v-model="queryParams.hzxm"
              placeholder="请输入户主姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleTestQuery">测试</el-button>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleTestAdminQuery">测试Admin</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['tdqq:nh:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['tdqq:nh:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['tdqq:nh:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['tdqq:nh:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="nhList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="农户代码" align="center" prop="nhdm" />
          <el-table-column label="户主姓名" align="center" prop="hzxm" />
          <el-table-column label="所属村组" align="center" prop="deptName" />
          <el-table-column label="是否成员" align="center" prop="sfzzcy" width="80">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.sfzzcy"/>
            </template>
          </el-table-column>
          <el-table-column label="证件号码" align="center" prop="zjhm" />
          <el-table-column label="家庭地址" align="center" prop="jtdz" />
          <el-table-column label="家庭人口" align="center" prop="jtrk" width="80"/>
          <el-table-column label="农户状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.mp_nhzt" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-user"
                @click="handleFamily(scope.row)"
                v-hasPermi="['house:nh:list']"
              >成员</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(scope.row)"
                v-hasPermi="['tdqq:nh:query']"
              >查看</el-button>
              <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['house:nh:edit', 'house:nh:remove']">
                <span class="el-dropdown-link">
                  <i class="el-icon-d-arrow-right el-icon--right"></i>更多
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="handleUpdate" icon="el-icon-edit"
                                    v-hasPermi="['house:nh:edit']">修改</el-dropdown-item>
                  <el-dropdown-item command="handleDelete" icon="el-icon-delete"
                                    v-hasPermi="['house:nh:remove']">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改农户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="农户代码" prop="nhdm">
              <el-input v-model="form.nhdm" placeholder="请输入农户代码" :readonly="true" disable="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="户主姓名" prop="hzxm">
              <el-input v-model="form.hzxm" placeholder="请输入户主姓名" :readonly="preview_mode" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="证件类型" prop="zjlx">
              <el-select v-model="form.zjlx" placeholder="请选择证件类型" :disabled="preview_mode">
                <el-option
                  v-for="dict in dict.type.mp_zjlx"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="证件号码" prop="zjhm">
              <el-input v-model="form.zjhm" placeholder="请输入证件号码" :readonly="preview_mode" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="性别" prop="xb">
              <el-select v-model="form.xb" placeholder="请选择性别" :disabled="preview_mode">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组织成员">
              <el-radio-group v-model="form.sfzzcy" :disabled="preview_mode">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属村镇" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择所属村镇" :disabled="preview_mode" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="农户状态">
              <el-radio-group :disabled="preview_mode" v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.mp_nhzt"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="邮政编码" prop="yzbm">
              <el-input v-model="form.yzbm" placeholder="请输入邮政编码" :readonly="preview_mode" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="lxfs">
              <el-input v-model="form.lxfs" placeholder="请输入联系方式" :readonly="preview_mode" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="调查日期" prop="dcrq">
              <el-input v-model="form.dcrq" placeholder="请输入调查日期" :readonly="preview_mode" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调查员" prop="lxfs">
              <el-input v-model="form.dcy" placeholder="请输入调查员" :readonly="preview_mode" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="家庭地址" prop="jtdz">
          <el-input v-model="form.jtdz" placeholder="请输入家庭地址" :readonly="preview_mode" />
        </el-form-item>
        <el-form-item label="调查记事" prop="dcjs">
          <el-input v-model="form.dcjs" type="textarea" placeholder="请输入调查记事" :readonly="preview_mode" />
        </el-form-item>
        <el-form-item label="备注" prop="bz">
          <el-input v-model="form.bz" type="textarea" placeholder="请输入备注" :readonly="preview_mode" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  :loading="buttonLoading" type="primary" @click="submitForm" v-if="opType !==2 && !preview_mode">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--    户内成员列表对话框-->
    <el-dialog :title="title_hncy" :visible.sync="open_hncy" width="1200px" append-to-body>
      <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAddHncy"
              v-hasPermi="['house:nhhncy:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdateHncy"
              v-hasPermi="['house:nhhncy:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDeleteHncy"
              v-hasPermi="['house:nhhncy:remove']"
            >删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="handleFamily"></right-toolbar>
      </el-row>
      <el-table v-loading="loading_hncy" :data="nhhncyList" @selection-change="handleHncySelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!--        <el-table-column label="成员ID" align="center" prop="cyId" v-if="true"/>-->
        <el-table-column label="姓名" align="center" prop="xm" />
        <el-table-column label="证件类型" align="center" prop="zjlx">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.mp_zjlx" :value="scope.row.zjlx"/>
          </template>
        </el-table-column>
        <el-table-column label="证件号码" align="center" prop="zjhm" />
        <el-table-column label="性别" align="center" prop="xb">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.xb"/>
          </template>
        </el-table-column>
        <el-table-column label="与户主关系" align="center" prop="yhzgx">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.mp_yhzgx" :value="scope.row.yhzgx"/>
          </template>
        </el-table-column>
        <!--        <el-table-column label="户口类型" align="center" prop="hklx">-->
        <!--          <template slot-scope="scope">-->
        <!--            <dict-tag :options="dict.type.mp_hklx" :value="scope.row.hklx"/>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="联系电话" align="center" prop="lxdh" />
        <el-table-column label="婚姻状况" align="center" prop="hyzk">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.mp_hyzk" :value="scope.row.hyzk"/>
          </template>
        </el-table-column>
        <!--        <el-table-column label="成员备注" align="center" prop="cybz" />-->
        <!--        <el-table-column label="独生子女" align="center" prop="dszn">-->
        <!--          <template slot-scope="scope">-->
        <!--            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.dszn"/>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="有住房" align="center" prop="yzf">-->
        <!--          <template slot-scope="scope">-->
        <!--            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.yzf"/>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="已购房" align="center" prop="ygf">-->
        <!--          <template slot-scope="scope">-->
        <!--            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.ygf"/>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="备注" align="center" prop="bz" />-->
        <!--        <el-table-column label="农户代码" align="center" prop="nhdm" />-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope_hncy">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleViewHncy(scope_hncy.row)"
              v-hasPermi="['house:nhhncy:query']"
            >查看</el-button>
            <!--            <el-button
                          size="mini"
                          type="text"
                          icon="el-icon-edit"
                          @click="handleUpdateHncy(scope_hncy.row)"
                          v-hasPermi="['house:nhhncy:edit']"
                        >修改</el-button>-->
            <!--            <el-button
                          size="mini"
                          type="text"
                          icon="el-icon-delete"
                          @click="handleDeleteHncy(scope_hncy.row)"
                          v-hasPermi="['house:nhhncy:remove']"
                        >删除</el-button>-->
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- 添加或修改户内成员对话框 -->
    <el-dialog :title="title_hncy_detail" :visible.sync="open_hncy_detail" width="800px" append-to-body>
      <el-form ref="form_hncy" :model="form_hncy" :rules="rules_hncy" label-width="150px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="xm">
              <el-input v-model="form_hncy.xm" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="xb">
              <el-select v-model="form_hncy.xb" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="证件类型" prop="zjlx">
              <el-select v-model="form_hncy.zjlx" placeholder="请选择证件类型">
                <el-option
                  v-for="dict in dict.type.mp_zjlx"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="证件号码" prop="zjhm">
              <el-input v-model="form_hncy.zjhm" placeholder="请输入证件号码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="与户主关系" prop="yhzgx">
              <el-select v-model="form_hncy.yhzgx" placeholder="请选择与户主关系">
                <el-option
                  v-for="dict in dict.type.mp_yhzgx"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="户口类型" prop="hklx">
              <el-select v-model="form_hncy.hklx" placeholder="请选择户口类型">
                <el-option
                  v-for="dict in dict.type.mp_hklx"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="婚姻状况" prop="hyzk">
              <el-select v-model="form_hncy.hyzk" placeholder="请选择婚姻状况">
                <el-option
                  v-for="dict in dict.type.mp_hyzk"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label="是否独生子女" prop="dszn">
              <el-radio-group v-model="form_hncy.dszn" placeholder="请选择是否独生子女">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="已在城镇购房" prop="ygf">
              <el-radio-group v-model="form_hncy.ygf">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他地方有住房" prop="yzf">
              <el-radio-group v-model="form_hncy.yzf" placeholder="请选择其他地方是否有住房">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="联系电话" prop="lxdh">
          <el-input v-model="form_hncy.lxdh" placeholder="请输入联系电话" />
        </el-form-item>

        <el-form-item label="成员备注" prop="cybz">
          <el-input v-model="form_hncy.cybz" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="bz">
          <el-input v-model="form_hncy.bz" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading_hncy" type="primary" @click="submitHncyForm">确 定</el-button>
        <el-button @click="cancel_hncy">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNh, getNh, delNh, addNh, updateNh, getUser,getAdminUser } from "@/api/tdqq/nh"
import { listNhhncy, getNhhncy, delNhhncy, addNhhncy, updateNhhncy } from "@/api/tdqq/nhhncy";
import { deptTreeSelect } from "@/components/DeptTdqqTree/api";
import DeptTdqqTree from "@/components/DeptTdqqTree/index.vue";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Nh",
  dicts: ['sys_yes_no', 'mp_zjlx', 'sys_user_sex', 'mp_nhzt','mp_hyzk',  'mp_yhzgx', 'mp_hklx'],
  components: { DeptTdqqTree,Treeselect},
  data() {
    return {
      // 遮罩层
      loading: false,
      buttonLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 农户信息表格数据
      nhList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opType: 0,
      preview_mode: false,
      // 部门树选项
      deptOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nhdm: null,
        hzxm: null,
        sfhz: null,
        sfzzcy: null,
        zjlx: null,
        zjhm: null,
        xb: null,
        jtdz: null,
        lxfs: null,
        txdz: null,
        deptId: null,
        jtrk: null,
        bz: null,
        hkbh: null,
        zgqzt: null,
        sfyyzjd: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hzxm: [
          { required: true, message: "户主姓名不能为空", trigger: "blur" }
        ],
        zjlx: [
          { required: true, message: "证件类型不能为空", trigger: "blur" }
        ],
        zjhm: [
          { required: true, message: "证件号码不能为空", trigger: "blur" }
        ],
      },

      // 查询参数
      queryParams_hncy: {
        pageNum: 1,
        pageSize: 100,
        xm: undefined,
        nhdm: undefined,
      },

      open_hncy: false,
      // loading_hncy: false,
      title_hncy: "",
      nhhncyList:[],
      loading_hncy:false,
      buttonLoading_hncy:false,
      open_hncy_detail: false,
      // loading_hncy_detail: false,
      title_hncy_detail: "",
      form_hncy: {},
      rules_hncy: {
        // cyId: [
        //   { required: true, message: "成员ID不能为空", trigger: "blur" }
        // ],
        xm: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        zjlx: [
          { required: true, message: "证件类型不能为空", trigger: "change" }
        ],
        /*        zjhm: [
                  { required: true, message: "证件号码不能为空", trigger: "blur" }
                ],*/
        xb: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        yhzgx: [
          { required: true, message: "与户主关系不能为空", trigger: "change" }
        ],
        // hklx: [
        //   { required: true, message: "户口类型不能为空", trigger: "change" }
        // ],
        // lxdh: [
        //   { required: true, message: "联系电话不能为空", trigger: "blur" }
        // ],
        // hyzk: [
        //   { required: true, message: "婚姻状况不能为空", trigger: "change" }
        // ],
        // cybz: [
        //   { required: true, message: "成员备注不能为空", trigger: "blur" }
        // ],
        // dszn: [
        //   { required: true, message: "是否独生子女不能为空", trigger: "change" }
        // ],
        // yzf: [
        //   { required: true, message: "其他地方是否有住房不能为空", trigger: "change" }
        // ],
        // ygf: [
        //   { required: true, message: "是否已在城镇购房不能为空", trigger: "change" }
        // ],
        // bz: [
        //   { required: true, message: "备注不能为空", trigger: "blur" }
        // ],
        // nhdm: [
        //   { required: true, message: "农户代码不能为空", trigger: "blur" }
        // ],
      },
    }
  },
  created() {
    this.getDeptTree()
  },
  methods: {
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleUpdate":
          this.handleUpdate(row);
          break;
        case "handleDelete":
          this.handleDelete(row);
          break;
        default:
          break;
      }
    },
    /** 删除按钮操作 */
    handleDeleteHncy(row) {
      const cyIds = row.cyId || this.ids;
      this.$modal.confirm('是否确认删除户内成员编号为"' + cyIds + '"的数据项？').then(() => {
        this.loading_hncy = true;
        return delNhhncy(cyIds);
      }).then(() => {
        this.loading_hncy = false;
        row.nhdm=this.queryParams_hncy.nhdm;
        this.handleFamily(row);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading_hncy = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdateHncy(row) {
      this.loading_hncy = true;
      this.reset_hncy();
      const cyId = row.cyId || this.ids
      getNhhncy(cyId).then(response => {
        this.loading_hncy = false;
        this.form_hncy = response.data;
        this.open_hncy_detail = true;
        // this.open_hncy = false;
        this.title_hncy_detail = "修改户内成员";
      });
    },
    /** 新增按钮操作 */
    handleAddHncy() {
      this.reset_hncy();
      this.open_hncy_detail = true;
      this.title_hncy_detail = "添加户内成员";
    },
    // 表单重置
    reset_hncy() {
      this.form_hncy = {
        cyId: undefined,
        xm: undefined,
        zjlx: "01",
        zjhm: undefined,
        xb: undefined,
        yhzgx: undefined,
        hklx: "01",
        lxdh: undefined,
        hyzk: undefined,
        cybz: undefined,
        dszn: "N",
        yzf: "N",
        ygf: "N",
        bz: undefined,
        nhdm: this.queryParams_hncy.nhdm,
        delFlag: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined
      };
      this.resetForm("form_hncy");
    },
    /** 提交按钮 */
    submitHncyForm() {
      this.$refs["form_hncy"].validate(valid => {
        if (valid) {
          this.buttonLoading_hncy = true;
          let row = {};
          row.nhdm=this.queryParams_hncy.nhdm;
          if (this.form_hncy.cyId != null) {
            updateNhhncy(this.form_hncy).then(response => {
              this.open_hncy_detail = false;
              this.$modal.msgSuccess("修改成功");
              this.handleFamily(row);
              // this.loading_hncy = true;
            }).finally(() => {
              this.buttonLoading_hncy = false;
              // this.loading_hncy = false;
            });
          } else {
            // this.loading_hncy = true;
            addNhhncy(this.form_hncy).then(response => {
              this.open_hncy_detail = false;
              this.$modal.msgSuccess("新增成功");
              this.handleFamily(row);
            }).finally(() => {
              this.buttonLoading_hncy = false;
              // this.loading_hncy = false;
            });
          }
        }
      });
    },
    // 取消按钮
    cancel_hncy() {
      this.open_hncy_detail = false;
      this.reset_hncy();
    },
    /** 查询农户信息列表 */
    getList() {
      this.loading = true
      listNh(this.queryParams).then(response => {
        this.nhList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    onDeptIdChanged(data) {
      this.queryParams.deptId = data.id;
      this.deptName = data.label;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        nhId: null,
        nhdm: null,
        hzxm: null,
        sfhz: null,
        sfzzcy: null,
        zjlx: null,
        zjhm: null,
        xb: null,
        jtdz: null,
        lxfs: null,
        txdz: null,
        deptId: null,
        jtrk: null,
        bz: null,
        hkbh: null,
        zgqzt: null,
        sfyyzjd: null,
        createTime: null,
        createBy: null,
        status: null,
        delFlag: null,
        updateTime: null,
        updateBy: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleTestAdminQuery(){
      getAdminUser().then(response => {
        console.log( response.data);
      })
    },
    handleTestQuery(){
      getUser("admin").then(response => {
        console.log( response.data);
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.nhId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加农户信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const nhId = row.nhId || this.ids
      getNh(nhId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改农户信息"
      })
    },
    // 以下为户内成员信息
    /** 成员按钮操作 */
    handleFamily(row) {
      this.loading_hncy = true;
      this.queryParams_hncy.nhdm = row.nhdm;
      // this.$modal.msgSuccess(this.queryParams_hncy.nhdm);
      listNhhncy(this.queryParams_hncy).then(response => {
        this.nhhncyList = response.rows;
        // this.total_hncy = response.total;
        this.open_hncy = true;
        this.title_hncy = "户内成员信息查看:"+row.nhdm;
        this.loading_hncy = false;
      });
    },
    // 多选框选中数据
    handleHncySelectionChange(selection) {
      this.ids = selection.map(item => item.cyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.loading = true;
      this.reset();
      this.opType =2;
      //const nhId = row.nhId || this.ids
      getNh(row.nhdm).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.preview_mode=true
        this.title = "查看农户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.nhId != null) {
            updateNh(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addNh(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const nhIds = row.nhId || this.ids
      this.$modal.confirm('是否确认删除农户信息编号为"' + nhIds + '"的数据项？').then(function() {
        return delNh(nhIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tdqq/nh/export', {
        ...this.queryParams
      }, `nh_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
